﻿Imports System.Security.Cryptography
Imports System.Text
Imports System.IO

Public Class tripleDes
    Private v_secretKey As String
    Private v_data As String
    Private v_encryptedData As String
    Public Property secretKey() As String
        Get
            Return v_secretKey
        End Get
        Set(ByVal Value As String)
            v_secretKey = Value
        End Set
    End Property
    Public Property data() As String
        Get
            Return v_data
        End Get
        Set(ByVal Value As String)
            v_data = Value
        End Set
    End Property
    Public Property encryptedData() As String
        Get
            Return v_encryptedData
        End Get
        Set(ByVal Value As String)
            v_encryptedData = Value
        End Set
    End Property
    Public Function encryptdata() As Boolean
        Try
            Dim encrypted() As Byte
            Dim toEncrypt() As Byte
            Dim MyKey() As Byte
            Dim MyIV() As Byte

            Dim encryptor As ICryptoTransform
            Dim Tdes As New TripleDESCryptoServiceProvider
            Tdes.Padding = 2
            'set cipher mode
            Tdes.Mode = CipherMode.CBC
            'assign key
            Tdes.Key = Encoding.ASCII.GetBytes(v_secretKey)
            'set key
            MyKey = Tdes.Key
            'Get  IV.
            Tdes.IV = Encoding.ASCII.GetBytes("15240982")
            'set iv
            MyIV = Tdes.IV
            'CreateEncryptor for encryption later
            encryptor = Tdes.CreateEncryptor(MyKey, MyIV)
            Dim msEncrypt As New MemoryStream
            'get CryptoStream for encrypted data
            Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
            'Convert the data to a byte array.
            toEncrypt = Encoding.ASCII.GetBytes(v_data)
            'Write all data to the crypto stream and flush it.
            csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
            csEncrypt.FlushFinalBlock()
            'Get encrypted array of bytes.
            encrypted = msEncrypt.ToArray()
            v_encryptedData = Convert.ToBase64String(encrypted)
            Return True
        Catch
            v_encryptedData = ""
            Return False
        End Try


    End Function
    Public Function decryptdata() As Boolean
        Try
            Dim MyKey() As Byte
            Dim MyIV() As Byte
            Dim fromEncrypt() As Byte
            Dim encrypted() As Byte
            encrypted = Convert.FromBase64String(v_encryptedData)

            MyKey = Encoding.ASCII.GetBytes(v_secretKey)
            'set iv
            MyIV = Encoding.ASCII.GetBytes("15240982")
            ' Now decryption
            Dim Tdes As New TripleDESCryptoServiceProvider

            Dim decryptor As ICryptoTransform = Tdes.CreateDecryptor(MyKey, MyIV)

            Dim msDecrypt As New MemoryStream(encrypted)
            'get CryptoStream for decrypted data
            Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
            fromEncrypt = New Byte(encrypted.Length) {}
            'Read the data out of the crypto stream.
            csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)
            'Convert the byte array back into a string.
            v_data = Encoding.ASCII.GetString(fromEncrypt)
            If v_data.IndexOf(Chr(&H0)) Then
                v_data = v_data.Replace(Chr(&H0), "")
            End If

            Return True
        Catch
            v_data = ""
            Return False
        End Try
    End Function
End Class

